<template>
  <div class="closing">
    <div>
      <el-button size="mini" @click="handleGetUntreated">{{ $t('btn.reload') }}</el-button>
      <el-button type="primary" size="mini" @click="isClosingDialog=true">{{ $t('market.filter') }}</el-button>
      <el-button type="primary" size="mini" @click="handleExport">{{ $t('btn.toExcel') }}</el-button>
    </div>
    <div class="header">
      <div class="header-name">
        <span>{{ $t('market.period') }}</span>
        <el-input v-model="period" size="mini" disabled />
      </div>
      <!--      当前关账期间-->
      <dataRow />
    </div>
    <div class="table-content">
      <el-tabs v-model="activeName" @tab-click="tabClick">
        <el-tab-pane :label="$t('market.costDetail')" name="first">
          <jc-table
            v-loading="loading"
            :table-data="tableData"
            :table-header="tableHeader"
            :table-height="tableHeight"
            get-row-keys="UUID"
            serial
            sortable
            :tree-props="{children: 'detailList', hasChildren: 'hasChild'}"
          >
            <!--   来源单ju    -->
            <template v-slot:tagSlot="col">
              <el-link type="primary" @click="pushFrombill(col.scope.row)">{{ col.scope.row.frombillNo }}</el-link>
            </template>
            <!--   生产订单号    -->
            <template v-slot:btnState="col">
              <el-link type="primary" @click="pushmob(col.scope.row)">{{ col.scope.row.mobillNo }}</el-link>
            </template>
            <!--    物料编码    -->
            <template v-slot:orderSlot="col">
              <el-link type="primary" @click="jumpMateriel(col.scope.row)">
                {{ col.scope.row.fnumber }}
              </el-link>
            </template>
            <!--    子项物料编码    -->
            <template v-slot:text2Slot="col">
              <el-link type="primary" @click="jumpMateriel1(col.scope.row.childnumber)">
                {{ col.scope.row.childnumber }}
              </el-link>
            </template>
            <template v-slot:btnSlot="col">
              <el-tooltip placement="top" :open-delay="openDelay">
                <div slot="content">{{ col.scope.row.frombillNo }}</div>
                <span>{{ col.scope.row.famount }}</span>
              </el-tooltip>
            </template>
            <!-- 单价 -->
            <template v-slot:billSlot="col">
              <el-link type="primary" @click="priceSource(col.scope.row, 1)">{{ col.scope.row.fprice }}</el-link>
            </template>
          </jc-table>
          <jc-pagination
            :total="subDetails.total"
            :page.sync="subDetails.pageNum"
            :limit.sync="subDetails.pageSize"
            @pagination="handleGetUntreated"
          />
        </el-tab-pane>
        <el-tab-pane :label="$t('market.shareDetail')" name="two">
          <jc-table
            v-loading="loading"
            :table-data="tableData1"
            :table-header="tableHeader1"
            :table-height="tableHeight"
            serial
            sortable
          />
          <!--          <jc-pagination-->
          <!--            :total="subDetails.total"-->
          <!--            :page.sync="subDetails.pageNum"-->
          <!--            :limit.sync="subDetails.pageSize"-->
          <!--            @pagination="handleGetUntreated"-->
          <!--          />-->
        </el-tab-pane>
      </el-tabs>

    </div>
    <!-- 选择时间 -->
    <el-dialog
      v-dialogDrag
      :title="$t('market.checkPeriod')"
      model
      :visible.sync="isClosingDialog"
      :close-on-click-modal="false"
      width="600px"
      top="5vh"
      center
    >
      <div>
        <el-form :model="subDetails" size="mini" label-width="100px">
          <el-form-item :label="$t('market.startPeriod')">
            <el-date-picker
              v-model="subDetails.start"
              type="month"
              value-format="yyyy-MM"
            />
          </el-form-item>
          <el-form-item :label="$t('market.endPeriod')">
            <el-date-picker
              v-model="subDetails.end"
              type="month"
              value-format="yyyy-MM"
            />
          </el-form-item>
          <el-form-item :label="$t('market.frombillNunber')">
            <el-input v-model.trim="subDetails.frombillNo" />
          </el-form-item>
          <el-form-item :label="$t('market.fmaterial')">
            <el-input v-model.trim="subDetails.materialNumber" />
          </el-form-item>
          <el-form-item :label="$t('market.stockname')">
            <el-input v-model.trim="subDetails.stockName" />
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isClosingDialog = false">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" @click="initialCon">{{ $t('btn.confirm') }}</el-button>
      </div>
    </el-dialog>
    <!-- 点击单价--来源 1 -->
    <el-dialog
      v-dialogDrag
      :title="$t('market.priceSource')"
      :visible.sync="isPriceDialog"
      :close-on-click-modal="false"
      width="60%"
      top="5vh"
      modal
      center
    >
      <div>
        <jc-table
          :table-header="tableHeader3"
          :table-data="tableData3"
          :get-summaries="getSummaries"
          show-summary
        >
          <template v-slot:billSlot="col">
            <span v-if="col.scope.row.fjump===0">{{ $t('market.zcp') }}</span>
            <span v-else-if="col.scope.row.fjump===1">{{ $t('market.ycl') }}</span>
            <span v-else-if="col.scope.row.fjump===2">{{ $t('market.caPrdcost') }}</span>
            <span v-else-if="col.scope.row.fjump===3">{{ $t('market.caSubcost') }}</span>
            <span v-else-if="col.scope.row.fjump===4">{{ $t('market.caMakecost') }}</span>
          </template>
          <template v-slot:btnSlot="col">
            <el-button v-if="col.scope.row.fjump===0" size="mini" type="primary" @click="priceSource(col.scope.row, 2)">{{ $t('market.priceSource') }}</el-button>
            <el-button v-else-if="col.scope.row.fjump===1" size="mini" type="primary" @click="pushView(col.scope.row)">{{ $t('market.inventory') }}</el-button>
          </template>
        </jc-table>
      </div>
    </el-dialog>
    <!-- 点击单价--来源 2 -->
    <el-dialog
      v-dialogDrag
      :title="$t('market.priceSource')"
      :visible.sync="isPriceDialog2"
      :close-on-click-modal="false"
      width="60%"
      top="5vh"
      modal
      center
    >
      <div>
        <jc-table
          :table-header="tableHeader3"
          :table-data="priceData2"
          :get-summaries="getSummaries"
          show-summary
        >
          <template v-slot:billSlot="col">
            <span v-if="col.scope.row.fjump===0">{{ $t('market.zcp') }}</span>
            <span v-else-if="col.scope.row.fjump===1">{{ $t('market.ycl') }}</span>
            <span v-else-if="col.scope.row.fjump===2">{{ $t('market.caPrdcost') }}</span>
            <span v-else-if="col.scope.row.fjump===3">{{ $t('market.caSubcost') }}</span>
            <span v-else-if="col.scope.row.fjump===4">{{ $t('market.caMakecost') }}</span>
          </template>
          <template v-slot:btnSlot="col">
            <el-button v-if="col.scope.row.fjump===0" size="mini" type="primary" @click="priceSource(col.scope.row, 3)">{{ $t('market.priceSource') }}</el-button>
            <el-button v-else-if="col.scope.row.fjump===1" size="mini" type="primary" @click="pushView(col.scope.row)">{{ $t('market.inventory') }}</el-button>
          </template>
        </jc-table>
      </div>
    </el-dialog>
    <!-- 点击单价--来源 3 -->
    <el-dialog
      v-dialogDrag
      :title="$t('market.priceSource')"
      :visible.sync="isPriceDialog3"
      :close-on-click-modal="false"
      width="60%"
      top="5vh"
      modal
      center
    >
      <div>
        <jc-table
          :table-header="tableHeader3"
          :table-data="priceData3"
          :get-summaries="getSummaries"
          show-summary
        >
          <template v-slot:billSlot="col">
            <span v-if="col.scope.row.fjump===0">{{ $t('market.zcp') }}</span>
            <span v-else-if="col.scope.row.fjump===1">{{ $t('market.ycl') }}</span>
            <span v-else-if="col.scope.row.fjump===2">{{ $t('market.caPrdcost') }}</span>
            <span v-else-if="col.scope.row.fjump===3">{{ $t('market.caSubcost') }}</span>
            <span v-else-if="col.scope.row.fjump===4">{{ $t('market.caMakecost') }}</span>
          </template>
          <template v-slot:btnSlot="col">
            <!--   <el-button v-if="col.scope.row.fjump===0" size="mini" type="primary" @click="priceSource(col.scope.row, 4)">{{ $t('market.priceSource') }}</el-button>-->
            <el-button v-if="col.scope.row.fjump===1" size="mini" type="primary" @click="pushView(col.scope.row)">{{ $t('market.inventory') }}</el-button>
          </template>
        </jc-table>
      </div>
    </el-dialog>
    <el-dialog
      v-dialogDrag
      :title="$t('menu.inventoryDetail')"
      :visible.sync="isInventory"
      :close-on-click-modal="false"
      width="60%"
      modal
      top="5vh"
      center
    > <!--存货收发明细表-->
      <inventoryDetail style="padding: 0" :data-sub="dataSub" />
    </el-dialog>
  </div>
</template>

<script>
import {
  queryCostCalculationDetail,
  queryCostCalculationList, queryCostShareList, queryRedirectUrl
} from '@/api/financial/warehousing'
import dataRow from '@/views/financial/company/components/dataRow'
import searData from '@/components/Search/mixin'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'

export default {
  name: 'SettiementList',
  components: {
    dataRow,
    inventoryDetail: () => import('@/views/financial/costAccounting/inventoryDetail')
  },
  mixins: [searData],
  data() {
    return {
      dataSub: {},
      isInventory: false,
      isPriceDialog: false,
      activeName: 'first',
      subDetails: {
        start: '',
        end: '',
        stockName: '',
        materialNumber: '',
        frombillNo: '',
        pageSize: 20,
        pageNum: 1,
        total: 0
      },
      openDelay: 1000,
      period: '',
      loading: false,
      isClosingDialog: false,
      // 表头
      tableData: [], // 存货收发存明细表
      tableHeight: '70vh',
      tableHeader: [
        { label: this.$t('market.period'), prop: 'fperiod', align: 'left' },
        { label: this.$t('market.deaprt'), prop: 'fdeptName' },
        { label: this.$t('market.frombillNo'), type: 'tag', prop: 'frombillNo', width: '120' },
        { label: this.$t('market.paezText1'), prop: 'salkhddh', width: '110' },
        { label: this.$t('market.mobillNo'), type: 'state', prop: 'mobillNo', width: '100' },
        { label: this.$t('market.proSeq'), prop: 'moSeq', width: '80' },
        { label: this.$t('market.productNo'), type: 'order', prop: 'fnumber', width: '120' },
        { label: this.$t('market.productName'), prop: 'fname' },
        { label: this.$t('market.productmolde'), prop: 'fmodel', width: '150' },
        { label: this.$t('market.funit'), prop: 'funitName', width: '80' },
        { label: this.$t('market.stock2'), prop: 'fstockName' },
        { label: this.$t('market.caStuffcost'), prop: 'caStuffcost' },
        { label: this.$t('market.caPrdcost'), prop: 'caPrdcost' },
        { label: this.$t('market.caSubcost'), prop: 'caSubcost' },
        { label: this.$t('market.caMakecost'), prop: 'caMakecost' },
        { label: this.$t('market.subitemNum'), type: 'text2', prop: 'childnumber', width: '120' },
        { label: this.$t('market.subitemName'), prop: 'childname', width: '120' },
        { label: this.$t('market.childModel'), prop: 'childmodel', width: '120' },
        { label: this.$t('market.childstock'), prop: 'childstockName', width: '120' },
        { label: this.$t('market.costItem'), prop: 'itemName', width: '120' },
        { label: this.$t('market.fqty'), prop: 'fqty', width: '80', align: 'right' },
        { label: this.$t('market.billamount'), type: 'btn', prop: 'famount', align: 'right' },
        { label: this.$t('market.fprice'), type: 'bill', prop: 'fprice', align: 'right' }
      ],
      tableData1: [],
      tableHeader1: [
        { label: this.$t('market.period'), prop: 'fperiod' },
        { label: this.$t('market.deaprt'), prop: 'fdeptName' },
        { label: this.$t('market.fqty'), prop: 'fqty', align: 'right' },
        { label: this.$t('market.laborcost'), prop: 'prdamount' },
        { label: this.$t('market.collectPrdcost'), prop: 'collectPrdcost' },
        { label: this.$t('market.collectSubcost'), prop: 'collectSubcost' },
        { label: this.$t('market.collectMakecost'), prop: 'collectMakecost' }
      ],
      tableData3: [],
      tableHeader3: [
        { label: this.$t('market.billType'), prop: 'fbillnoname' },
        { label: this.$t('market.billNo'), prop: 'fbillno' },
        { label: this.$t('shop.fnumber1'), prop: 'fnumber', width: '120' },
        { label: this.$t('shop.fname2'), prop: 'fname' },
        { label: this.$t('shop.fspecificaTion'), prop: 'fspecification' },
        { label: this.$t('shop.fdescripTion'), prop: 'fdescription' },
        { label: this.$t('market.stock2'), prop: 'fstock', width: '120' },
        { label: this.$t('market.fqty'), prop: 'fqty', width: '80' },
        { label: this.$t('market.priceMob'), prop: 'fprice', width: '80' },
        { label: this.$t('market.famount'), prop: 'rawAmount', width: '80' },
        { label: this.$t('market.materLedger'), type: 'bill', width: '80' },
        { label: this.$t('shop.operation'), type: 'btn', width: '120' }
      ],
      isPriceDialog2: false,
      isPriceDialog3: false,
      priceData2: [],
      priceData3: []
    }
  },
  created() {
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 260px)' : '73vh'
  },
  methods: {
    // 过滤确认
    initialCon() {
      this.isClosingDialog = false
      const { start, end } = this.subDetails
      this.period = start + ' -- ' + end
      this.handleGetUntreated()
    },
    // 获取成本结算单列表数据
    async handleGetUntreated() {
      this.loading = true
      const DATA = { ...this.subDetails }
      await queryCostCalculationList(DATA).then(RES => {
        if (RES.code === 0) {
          this.loading = false
          this.tableData = RES.data.list
          this.tableData.map(item => {
            item.fprice = (item.famount / item.fqty).toFixed(4)
            // 遍历
            this.detailPrice(item.detailList)
          })
          this.subDetails.total = RES.data.total
        } else {
          this.loading = false
          this.$confirm(RES.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 遍历 + 单价
    detailPrice(item) {
      item?.length > 0 && item.forEach(ite => {
        if (ite.fqty > 0) ite.fprice = (ite.famount / ite.fqty).toFixed(4)
        this.detailPrice(ite.detailList)
      })
    },
    // 搜索
    handleQuerySonClass() {
      this.currentPage = 1
      this.handleGetUntreated()
    },
    // 跳转生产委外订单
    pushmob(ev) {
      const { fbilltypeId, mobillNo } = ev
      if (fbilltypeId === 8) { // 生产
        this.$router.push({ path: `/proOrderDetails/${mobillNo}` })
      } else if (fbilltypeId === 20) { // fbilltypeId === 20 委外
        this.$router.push({ path: `/outOrderDetails/${mobillNo}` })
      }
    },
    // 来源单据跳转页面
    pushFrombill(ev) {
      const DATA = {
        fbillNo: ev.frombillNo,
        enumId: ev.fbilltypeId
      }
      queryRedirectUrl(DATA).then(res => {
        if (res.code === 0) {
          this.$router.push({ path: res.data.redirectUrl })
        }
      })
    },
    tabClick(tab) {
      if (tab.name === 'two') {
        this.costShareList()
      }
    },
    // 分摊明细
    costShareList() {
      const { start, end } = this.subDetails
      const DATA = {
        start: start,
        end: end
      }
      queryCostShareList(DATA).then(res => {
        if (res.code === 0) {
          this.tableData1 = res.data.list
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 跳转存货收发明细表
    pushView(ev) {
      this.isInventory = true
      this.dataSub = {
        fnumber: ev.fnumber,
        start: this.subDetails.start,
        end: this.subDetails.end
      }
    },
    // 价格来源
    priceSource(ev, en) {
      switch (en) {
        case 1: this.isPriceDialog = true
          break
        case 2: this.isPriceDialog2 = true
          break
        case 3: this.isPriceDialog3 = true
          break
      }
      const DATA = {
        fbilltypeId: ev.fbilltypeId,
        fmaterialId: ev.fmaterialId,
        frombillNo: ev.frombillNo,
        mobillNo: ev.mobillNo,
        moSeq: ev.moSeq,
        fqty: ev.moqty || ev.fqty
      }
      queryCostCalculationDetail(DATA).then(res => {
        if (res.code === 0) {
          switch (en) {
            case 1: this.tableData3 = res.data
              break
            case 2: this.priceData2 = res.data
              break
            case 3: this.priceData3 = res.data
              break
          }
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 导出表格
    handleExport() {
      require.ensure([], () => {
        const tHeader = this.tableHeader.map((res) => {
          return res.label
        })
        const filterVal = this.tableHeader.map((res) => {
          return res.prop
        })
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else {
          queryCostCalculationList({
            ...this.subDetails,
            pageSize: 100000,
            pageNum: 1
          }).then(res => {
            if (res.code === 0) {
              res.data.list.map(item => {
                item.fprice = (item.famount / item.fqty).toFixed(4)
              })
              const list = res.data.list
              const data = formatJson(filterVal, list)
              export_json_to_excel(tHeader, data, '完工入库列表')
            } else {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.closing {
  @include listBom;
  .el-input::v-deep .el-input__inner {
    height: 30px;
    line-height: 30px;
  }
  .header{
    margin-top: 10px;
    .header-name{
      span {
        width: 100px;
        margin-right: 10px;
        text-align: right;
      }
    }
  }
}
</style>
